package com.sisy.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.sisy.demo.dao.UserMapper;
import com.sisy.demo.entity.User;
import com.sisy.demo.exception.BaseException;
import com.sisy.demo.service.UserService;
import com.sisy.demo.utils.Encryption;
import com.sisy.demo.vo.UserRegisterVO;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description: 用户接口
 * @author: xx
 * @date: 2020-06-05 15:25
 * @version: 0.0.1
 * @Copyright: 2020 www.jztdata.com Inc. All rights reserved.
 * 注意：本内容仅限于杭州金智塔科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void register(UserRegisterVO vo) {
        User user = userMapper.selectOne(
                new QueryWrapper<User>()
                        .eq("phone", vo.getPhone()));
        if (user != null) {
            throw new BaseException("手机号码已存在");
        }
        user = new User();
        user.setPhone(vo.getPhone());
        user.setPassword(Encryption.encoderMD5(vo.getPwd()));
        user.setGender(vo.getGender());
        user.setNickName(vo.getNickName());
        int insert = userMapper.insert(user);
        if (insert == 0) {
            throw new BaseException("注册异常");
        }
    }

    @Override
//    @RabbitListener(queues = "blog_queue")
//    @RabbitListener(queues = "blog_queue", containerFactory = "workListenerFactory")
    public void queueTest(String message) {

        try {
            Thread.sleep(4000);
            System.out.println( Thread.currentThread().getName()+" Receiver——001 " + message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    @RabbitListener(queues = "blog_queue", containerFactory = "workListenerFactory")
//    @RabbitListener(queues = "blog_queue")
    public void queueTest2(String message) {
        try {
//            Thread.sleep(4000);
            System.out.println(Thread.currentThread().getName()+" Receiver——002 " + message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


//    @RabbitListener(queues = "#{autoDeleteQueue1.name}")
    public int fanoutQueue01(int message) {
        System.out.println("queue01:" + message);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return message + 10;
    }

//    @RabbitListener(queues = "#{autoDeleteQueue2.name}")
//    public void fanoutQueue02(String message) {
//        System.out.println("queue02:" + message);
//    }
}
